<?php
namespace Admin\Controller;
use Think\Controller;
class PositioncontentController extends CommonController{
	public function index()
	{
		//// 获取推荐位内容 
		$positions = D('Position')->getNormalPositions();
		
		$data['status'] = ['neq',-1];
		if($_GET['title']){
			$data['title'] = trim($_GET['title']);
			$this->assign('title',$data['title']);
		}
		$data['position_id'] ? intval($_GET['position_id']) : $positions[0]['id'];
		// var_dump($positions);die;
		// 分页操作
       	$page = $_REQUEST['p'] ? $_REQUEST['p'] : 1;
       	$pageSize = $_REQUEST['pageSize'] ? $_REQUEST['pageSize'] : 3;
       	$contents = D('PositionContent')->getMenus($data,$page,$pageSize);
       	// 
       	$menusCount = D('PositionContent')->getMenusCount($data);
       	$res = new \Think\Page($menusCount,$pageSize);
       	$pageRes = $res->show();
       	// var_dump($contents);die;
		// $contents = D("PositionContent")->select($data);
		$this->assign('contents',$contents);
		$this->assign('positions',$positions);
		$this->assign('positionId',$data['position_id']);
		$this->assign('pageRes',$pageRes);
		$this->display();
	}
	public function add()
	{
		if($_POST){
			// var_dump($_POST);die;
			if(!isset($_POST['position_id']) || !$_POST['position_id']){
				return show(0,'推荐位ID不能为空');
			}
			if(!isset($_POST['title']) || !$_POST['title']){
				return show(0,'推荐位标题不能为空');
			}
			if(!isset($_POST['url']) && !$_POST['news_id']){
				return show(0,'url和news_id不能同时为空');
			}
			if(!isset($_POST['thumb']) || !$_POST['thumb']){
				if($_POST['news_id']){
					// 获取文章的图片
					$res = D("News")->find($_POST['news_id']);
					// var_dump($res);die;
					if($res && is_array($res)){
						$_POST['thumb'] = $res['thumb'];
					}else{
						return show(0,'图片不能为空');
					}
				}
			}
			// 执行更新的操作
			if($_POST['id']){
				return $this->save($_POST);
			}
			try{
				// 执行推荐位内容的插入
				$id = D("PositionContent")->insert($_POST);
				if($id){
					return show(1,'新增成功');
				}
				return show(0,'新增失败');
			}catch(Exception $e){
				return show(0,$e->getMessage());
			}
		}else{
			// 获取推荐位的内容
			$positions = D("Position")->getNormalPositions();
			$this->assign('positions',$positions);
			$this->display();
		}
	}
	// 修改操作的显示页面
	public function edit($id)
	{
		// 查看推荐位的信息
		$position = D("PositionContent")->find($id);
		 // 获取推荐位的内容
		$positions = D("Position")->getNormalPositions();
		$this->assign('vo',$position);
		$this->assign('positions',$positions);
		$this->display();
	}
	public function save()
	{
		$id = $data['id'];
		unset($data['id']);
		try{
			// 执行修改操作
			$resId = D("PositionContent")->updateById($id,$data);
			if($resId === false){
				return show(0,'更新失败');
			}
			return show(1,'更新成功');
		}catch(Exception $e){
			return show(0,$e->getMessage());
		}
	}
	// 修改状态
	public function setStatus(){
		$data = [
			'id' => intval($_POST['id']),
			'status' => intval($_POST['status']),
		];
		return parent::setStatus($data,'PositionContent');
	}
	// 排序操作
	public function listorder()
	{
		return parent::listorders("PositionContent");
	}
}